package com.dengjiawen.project01.helper;

import com.dengjiawen.project01.bag.impl.MyObjectListImpl;
import com.dengjiawen.project01.util.FileUtil;

import java.io.IOException;

/**
 * Init the data of the impl class,and be called by Driver class.
 * the data file [data.txt] is is another dir ,named [resources].
 * dir resources Under the main folder
 *
 * @author Jarvan
 * @version 1.0
 * @create 2020/9/22 20:52
 */
public class Helper {

    /**
     * This void method is decomposed.
     * It creates an empty list.
     * It calls the create method to add a list of objects to the list.
     * And then, it calls the display method to display the list of objects.
     */
    public static void start() throws IOException {
        MyObjectListImpl myObjectList = create("data.txt");
        display(myObjectList);
    }

    /**
     * This method creates a list of objects using data stored in text files.
     *
     * @return MyObjectListImpl that have 5 items.
     */
    public static MyObjectListImpl create(String resourcesFileName) throws IOException {
        String s = FileUtil.readFileAsString(resourcesFileName);
        String[] splits = s.split("//n");
        return new MyObjectListImpl(splits);
    }

    /**
     * This method displays a list of objects.
     *
     * @return
     */
    public static void display(MyObjectListImpl myObjectList) {
        Object[] list = myObjectList.getList();
        for (int i = 0; i < myObjectList.size(); i++) {
            System.out.println(list[i]);
        }
    }
}
